from .. import models
from django.urls import reverse
from rest_framework import serializers
from .lib import ProjectSubSerializer, ProjectSubWithAuditSerializer

class ProjectListSerializer(serializers.ModelSerializer):
    creator = serializers.SerializerMethodField()
    link = serializers.SerializerMethodField()

    overall_audit_status = serializers.SerializerMethodField()
    overall_audit_progress = serializers.SerializerMethodField()
    
    class Meta:
        model = models.Project
        fields = ['id', 'chinese_name', 'english_name', 'drug_name', 'creator',
                  'created_at', 'overall_audit_status', 'overall_audit_progress', 'link']
        
    def get_creator(self, proj: models.Project):
        return str(proj.creator)

    def get_link(self, proj: models.Project):
        return reverse('project:detail', args=(proj.pk, ))

    def get_overall_audit_status(self, proj: models.Project):
        # Get prefetched audit records
        ams = getattr(proj, '_audit_managements', [])
        return proj._am_info(ams)[1]

    def get_overall_audit_progress(self, proj: models.Project):
        ams = getattr(proj, '_audit_managements', [])
        return proj._am_info(ams)[0]

class PlanListSerializer(ProjectSubWithAuditSerializer):
    class Meta:
        model = models.Plan
        exclude = ["project", "content"]

class InitiationDataListSerializer(ProjectSubWithAuditSerializer):
    class Meta:
        model = models.InitiationData
        exclude = ["project"]

class EthicalReviewSerializer(ProjectSubSerializer):
    class Meta:
        model = models.EthicalReview
        exclude = ["project"]
        
class HumanGeneticReousceListSerializer(ProjectSubSerializer):
    class Meta:
        model = models.HumanGeneticResource
        exclude = ["project"]

class ContractListSerializer(ProjectSubSerializer):
    class Meta:
        model = models.Contract
        exclude = ["project"]

class MaterialListSerializer(ProjectSubSerializer):
    class Meta:
        model = models.Material
        exclude = ["project"]

class QualityControlListSerializer(ProjectSubSerializer):
    class Meta:
        model = models.QualityControl
        exclude = ["project"]

class ExaminationListSerializer(ProjectSubSerializer):
    class Meta:
        model = models.Examination
        exclude = ["project"]
